// Copyright (c) Microsoft Corporation. All rights reserved.

namespace Microsoft.Tools.CodeRunner.UnitTest
{
	using System;
	using System.Text;
	using System.Collections.Generic;
	using System.IO;
	using System.Diagnostics;
	using System.Threading;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Microsoft.Tools.CodeRunner;

	[TestClass]
	public class RemoteEnvironmentTests 
	{
		private static TestContext context;

		public RemoteEnvironmentTests() 
		{
		}

		[ClassInitialize()]
		public static void ClassInit(TestContext context)
		{
			RemoteEnvironmentTests.context = context;
		}

		[DeploymentItem(@"UnitTest\PipeServer\PipeServer.exe")]
		[DeploymentItem(@"UnitTest\PipeClient\PipeClient.exe")]
		[TestMethod]
		public void TestPipeClientServer()
		{
            EventWaitHandle serverStartedEvent = new EventWaitHandle(false, EventResetMode.ManualReset, "CodeRunner.Test.ServerStarted.Event");

		    Process procServer = Process.Start("PipeServer.exe");
			
			// Wait for the server to signal that it started
			serverStartedEvent.WaitOne();
			
			Process procClient = Process.Start("PipeClient.exe");

			bool serverExit = procServer.WaitForExit(5000);
			bool clientExit = procClient.WaitForExit(5000);
			
			Assert.IsTrue(serverExit && clientExit);
		}

		[DeploymentItem(@"UnitTest\RemoteTest\RemoteTest.exe")]
		[TestMethod]
		public void TestRemoteEnvironment() 
		{
			Process proc = null;
			
			proc = Process.Start("RemoteTest.exe");

			// Inject once, and set a nothing variable
			RemoteEnvironment env = new RemoteEnvironment(proc.Id);
	
			env.SetEnvironmentVariable("CR_UTILITY_IGNORE", "stop");
			env.Dispose();

			// Inject again and set the stop variable
			env = new RemoteEnvironment(proc.Id);

			env.SetEnvironmentVariable("CR_UTILITY_REMOTE_TEST", "stop");
			env.Dispose();
			
			proc.WaitForExit();
		}
	}
}
